package com.lanou.pianke.search.service.impl;

import com.lanou.pianke.search.EntityRepository.UserRepository;
import com.lanou.pianke.search.entity.User;
import com.lanou.pianke.search.service.UserService;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public List<User> searchByUname(String searchContent) {
        QueryStringQueryBuilder builder = new QueryStringQueryBuilder(searchContent);
        Iterable<User> search = userRepository.search(builder);
        Iterator<User> iterator = search.iterator();
        List<User> preplyList = new ArrayList<>();
        while (iterator.hasNext()){
            preplyList.add(iterator.next());
        }
        return preplyList;
    }
}
